import { defineConfig } from 'vite'
import path, { resolve } from 'path'
import vue from '@vitejs/plugin-vue'
import viteCompression from 'vite-plugin-compression'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// import { viteMockServe } from 'vite-plugin-mock'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  console.log('wwwwwwwwwwwwww', mode)
  return {
    base: '/',
    // base: command === 'serve' ? '/' : '/vue3-element-admin/',
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      }),
      viteCompression({
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: 'gzip',
        ext: '.gz'
      })
      // viteMockServe({
      //   supportTs: false,
      //   mockPath: 'mock',
      //   localEnabled: command === 'serve',
      //   prodEnabled: command !== 'serve' && prodMock,
      //   injectCode: `
      //     import { setupProdMockServer } from './mockProdServer';
      //     setupProdMockServer();
      //   `
      // })
    ],
    resolve: {
      alias: {
        '@': resolve('./src'),
        '@img': resolve('./src/assets/img'),
        Utils: resolve('/src/utils'),
        Common: resolve('/src/components')
      }
    },
    css: {
      //   preprocessorOptions: {
      //     scss: {
      //       additionalData: `@import "./src/styles/variables.scss";`
      //     }
      //   }
      modules: {
        // generateScopedName: '[name]-[local]-[hash:base64:5]',
        hashPrefix: 'prefix'
      },
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          additionalData: `@import "${path.resolve(__dirname, 'src/styles/index.less')}";`
        }
      }
    },
    server: {
      host: '0.0.0.0',
      port: 3001,
      open: false
      // proxy: {
      //   '/api': {
      //     target: 'http://admin.xueyueob.cn/api',
      //     changeOrigin: true,
      //     ws: true,
      //     rewrite: (path) => path.replace(new RegExp('^/api'), '')
      //   }
      // }
    },
    build: {
      sourcemap: mode !== 'production', // 非生产模式打包引入sourcemap
      // manifest: true,
      rollupOptions: {
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
          manualChunks(id) {
            if (id.includes('node_modules')) {
              if (id.toString().split('node_modules/')[1].split('/')[0].includes('element-plus')) {
                return 'element-plus'
              } else {
                return 'wq'
              }
            }
          }
        }
      },
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      },
      chunkSizeWarningLimit: 500
    }
  }
})
